package com.cake.bigdata.starter.exception.category;

import com.cake.bigdata.starter.exception.error.CommonErrorCode;
import com.cake.bigdata.starter.exception.error.details.BusinessErrorCode;
import lombok.Getter;

/**
 * {@link RuntimeException} 通用业务异常
 *
 * @author gst
 * @date 2019-12-20 20:30
 */
@Getter
public class BusinessException extends RuntimeException {

    private String  code;
    private boolean isShowMsg = true;

    /**
     * 使用枚举传参
     *
     * @param errorCode 异常枚举
     */
    public BusinessException(BusinessErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
    }

    /**
     * 使用CommonErrorCode枚举传参
     *
     * @param errorCode 异常枚举
     */
    public BusinessException(CommonErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
    }

    /**
     * 使用自定义消息
     *
     * @param code 值
     * @param msg  详情
     */
    public BusinessException(String code, String msg) {
        super(msg);
        this.code = code;
    }

    /**
     * 使用自定义消息
     *
     * @param msg 详情
     */
    public BusinessException(String msg) {
        super(msg);
    }

    /**
     * 根据原生异常构造对象.
     *
     * @param cause 原生异常.
     */
    public BusinessException(final Throwable cause) {
        this(CommonErrorCode.EXCEPTION.getCode(), CommonErrorCode.EXCEPTION.getMessage(), cause);
    }

    /**
     * 根据异常信息和原生异常构造对象.
     *
     * @param code    错误码
     * @param message 异常信息.
     * @param cause   原生异常.
     */
    public BusinessException(final String code, final String message, final Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    /**
     * 根据异常信息和原生异常构造对象.
     *
     * @param message 异常信息.
     * @param cause   原生异常.
     */
    public BusinessException(final String message, final Throwable cause) {
        this(CommonErrorCode.EXCEPTION.getCode(), message, cause);
    }
}
